package com.one.group.admin.config;

import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.one.group.admin.mapper.ContractMapper;
import com.one.group.model.entity.Contract;
import com.one.group.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ContractStatusScheduler {
    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private ContractService contractService;
    // 每天凌晨1点执行
    @Scheduled(cron = "0 0 1 * * ?")
    public void updateExpiredContracts() {
        // 查询所有已到期但状态未变更的合同
        QueryWrapper queryWrapper = QueryWrapper.create().from(Contract.class);
        queryWrapper.eq(Contract::getContractStatus,1);
        List<Contract> list = contractService.list(queryWrapper);
        for (Contract contract:list){
            if (!contract.getEndDate().after(new Date())){
                Contract contract1 = new Contract();
                contract1.setContractStatus(1);
                contract1.setId(contract.getId());
                contractService.saveOrUpdate(contract1);
            }
        }


    }
}
